//////////////////////////////////////////////////////////////////////
// CLayer.cpp: implementatie van de CLayer klasse.
//////////////////////////////////////////////////////////////////////
#include "layer.h"

//////////////////////////////////////////////////////////////////
// Static Initialisatie
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// Constructors/Destructors
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// Member functies
//////////////////////////////////////////////////////////////////
CLayer::CLayer()
{
	m_pLayerSurface = 0;
	m_bTransparent	= TRUE;
	m_ColorKey		= D3DCOLOR_ARGB(0, 255, 0, 255);

	m_x				= 0;
	m_y				= 0;
	m_VelocityX		= 0;
	m_VelocityY		= 0;
	
	m_SurfaceWidth	= 0;
	m_SurfaceHeight	= 0;

	m_MoveDirection	= None;
}

CLayer::~CLayer()
{
	if (m_pLayerSurface)
	{
		m_pLayerSurface->Release();
		m_pLayerSurface = 0;
	}
}

void CLayer::SetColorKey(D3DCOLOR ColorKey)
{
	this->m_ColorKey = ColorKey;
}

D3DCOLOR CLayer::GetColorKey()
{
	return this->m_ColorKey;
}

void CLayer::SetVelocity(int vx, int vy)
{
	this->m_VelocityX = vx;
	this->m_VelocityY = vy;
}

void CLayer::GetVelocity(int * pvx, int * pvy)
{
	*pvx = this->m_VelocityX;
	*pvy = this->m_VelocityY;
}

void CLayer::SetPosition(int x, int y)
{
	this->m_x = x;
	this->m_y = y;
}

void CLayer::GetPosition(int * px, int * py)
{
	*px = this->m_x;
	*py = this->m_y;
}

void CLayer::Update()
{
	if (m_VelocityX != 0 && m_VelocityY == 0)
		m_MoveDirection = Horizontal;
	else if (m_VelocityX == 0 && m_VelocityY != 0)
		m_MoveDirection = Vertical;
	else if (m_VelocityX != 0 && m_VelocityY != 0)
		m_MoveDirection = Both;
	else if (m_VelocityX == 0 && m_VelocityY == 0)
	{
		m_MoveDirection = None;
		return;
	}

	m_x += m_VelocityX;
	m_y += m_VelocityY;

	if (m_x >= zen.m_nDeviceWidth)
		m_x -= zen.m_nDeviceWidth;

	if (m_x <= -zen.m_nDeviceWidth)
		m_x += zen.m_nDeviceWidth;

	if (m_y >= zen.m_nDeviceHeight)
		m_y -= zen.m_nDeviceHeight;

	if (m_y <= -zen.m_nDeviceHeight)
		m_y += zen.m_nDeviceHeight;
}

void CLayer::Shutdown()
{
	m_pLayerSurface = 0;
	m_bTransparent	= TRUE;
	m_ColorKey		= D3DCOLOR_ARGB( 0, 255, 0, 255 );

	m_x = m_y	= 0;
	m_VelocityX = 0;
	m_VelocityY = 0;

	m_SurfaceWidth	= 0;
	m_SurfaceHeight = 0;
	
	if( m_pLayerSurface )
	{
		m_pLayerSurface->Release();
		m_pLayerSurface = 0;
	}
}

HRESULT CLayer::Initialize(char * strPathName, BOOL bTransparent)
{
	HRESULT res;
	D3DSURFACE_DESC d3dsd;

	if (m_pLayerSurface)
	{
		m_pLayerSurface->Release();
		m_pLayerSurface = 0;
	}

	res = zen.LoadBitmapToSurface(strPathName, &m_pLayerSurface, zen.m_pDevice);
	if ( FAILED(res) )
		return E_FAIL;

	m_bTransparent	= bTransparent;
	m_pLayerSurface->GetDesc(&d3dsd);
	m_SurfaceWidth  = d3dsd.Width;
	m_SurfaceHeight = d3dsd.Height;

	return S_OK;
}

HRESULT CLayer::Render(LPDIRECT3DSURFACE8 pTargetSurface)
{
	int OffsetX;
	int OffsetY;
	D3DSURFACE_DESC d3dsd;

	//--------SECTION 1
	if (!pTargetSurface)
		return E_FAIL;

	pTargetSurface->GetDesc(&d3dsd);

	OffsetX = m_x;
	OffsetY = m_y;

	if (OffsetX < 0)
	{
		OffsetX = d3dsd.Width + m_x;
	}

	if (OffsetY < 0)
	{
		OffsetY = m_SurfaceHeight + OffsetY;
	}

	switch(m_MoveDirection)
	{
	//--------SECTION 2
		case None:
		{
			zen.CopySurfaceToSurface(0, m_pLayerSurface, 0, pTargetSurface, m_bTransparent, m_ColorKey);
		}
		break;

	//--------SECTION 3
		case Horizontal:
		{
			RECT RectL;
			RECT RectR;
			POINT DestPointR = {OffsetX, OffsetY};
			POINT DestPointL = {0, OffsetY};

			SetRect(&RectR, 0, 0, m_SurfaceWidth - OffsetX, m_SurfaceHeight);
			SetRect(&RectL, m_SurfaceWidth - OffsetX, 0, OffsetX, m_SurfaceHeight);

			zen.CopySurfaceToSurface(&RectL, m_pLayerSurface, &DestPointL, pTargetSurface,
									 m_bTransparent, m_ColorKey);
			zen.CopySurfaceToSurface(&RectR, m_pLayerSurface, &DestPointR, pTargetSurface,
									 m_bTransparent, m_ColorKey);
		}
		break;
	
	//--------SECTION 4
		case Vertical:
		{
			RECT RectT;
			RECT RectB;
			POINT DestPointT = {OffsetX, 0};
			POINT DestPointB = {OffsetX, OffsetY};

			SetRect(&RectT, 0, m_SurfaceHeight - OffsetY, m_SurfaceWidth, OffsetY);
			SetRect(&RectB, 0, 0, m_SurfaceWidth, m_SurfaceHeight - OffsetY);

			zen.CopySurfaceToSurface(&RectT, m_pLayerSurface, &DestPointT, pTargetSurface,
									 m_bTransparent, m_ColorKey);
			zen.CopySurfaceToSurface(&RectB, m_pLayerSurface, &DestPointB, pTargetSurface,
									 m_bTransparent, m_ColorKey);
		}
		break;

	//--------SECTION 5
		case Both:
		{
			RECT Rect0;
			RECT Rect1;
			RECT Rect2;
			RECT Rect3;

			POINT DestPoint0 = {OffsetX, OffsetY};
			POINT DestPoint1 = {OffsetX, 0};
			POINT DestPoint2 = {0, OffsetY};
			POINT DestPoint3 = {0, 0};

			SetRect(&Rect0, 0, 0, m_SurfaceWidth - OffsetX, m_SurfaceHeight - OffsetY);
			SetRect(&Rect1, 0, m_SurfaceHeight - OffsetY, m_SurfaceWidth - OffsetX, OffsetY);
			SetRect(&Rect2, m_SurfaceWidth - OffsetX, 0, OffsetX, m_SurfaceHeight - OffsetY);
			SetRect(&Rect3, m_SurfaceWidth - OffsetX, m_SurfaceHeight - OffsetY, OffsetX, OffsetY);

			zen.CopySurfaceToSurface(&Rect0, m_pLayerSurface, &DestPoint0, pTargetSurface,
									 m_bTransparent, m_ColorKey);
			zen.CopySurfaceToSurface(&Rect1, m_pLayerSurface, &DestPoint1, pTargetSurface,
									 m_bTransparent, m_ColorKey);
			zen.CopySurfaceToSurface(&Rect2, m_pLayerSurface, &DestPoint2, pTargetSurface,
									 m_bTransparent, m_ColorKey);
			zen.CopySurfaceToSurface(&Rect3, m_pLayerSurface, &DestPoint3, pTargetSurface,
									 m_bTransparent, m_ColorKey);
		}
		break;
	}

	return S_OK;
}
